package programmercarl.回溯.C5;

import java.util.ArrayList;
import java.util.List;

class Solution1 {

    List<String> ans = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return ans;
        }
        //初始对应所有的数字，为了直接对应2-9，新增了两个无效的字符串""
        String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        //迭代处理
        backTrack(digits, numString, 0);
        return ans;
    }

    StringBuilder sb = new StringBuilder();

    private void backTrack(String digits, String[] numString, int index) {
        if (index == digits.length()) {
            ans.add(sb.toString());
            return;
        }
        String currentStr = numString[digits.charAt(index)-'0'];
        for (int i = 0; i < currentStr.length(); i++) {
            sb.append(currentStr.charAt(i));
            backTrack(digits, numString, index + 1);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
}